﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Timers;

namespace dotSimpl.wlanner
{
    /// <summary>
    /// Interaction logic for PopUp.xaml
    /// </summary>
    public partial class PopUp : Window
    {
        #region Fields

        private Timer timerClose;
        private delegate void CloseDelegate();
        private CloseDelegate closeForm;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of pop-up window.
        /// </summary>
        /// <param name="autoClosePeriod">Period for auto-close in milliseconds.</param>
        public PopUp(int autoClosePeriod)
        {
            InitializeComponent();
            timerClose = new Timer(autoClosePeriod);
            closeForm = CloseForm;
            timerClose.Elapsed += new ElapsedEventHandler(timerClose_Elapsed);
        }

        /// <summary>
        /// Creates a new instance of pop-up window. With default delay (3 sec.)
        /// </summary>
        public PopUp() : this(3000) { }

        #endregion

        #region Methods

        /// <summary>
        /// Shows pop-up window with specified text.
        /// </summary>
        /// <param name="autoCloseDelay">Delay for window auto-closing.</param>
        /// <param name="text">Text to show in window.</param>
        public static void Show(int autoCloseDelay, string text)
        {
            PopUp popUp = new PopUp(autoCloseDelay);
            popUp.Topmost = true;
            popUp.tbText.Text = text;
            popUp.Show();
            popUp.Left = SystemParameters.PrimaryScreenWidth - popUp.Width - 30;
            popUp.Top = SystemParameters.PrimaryScreenHeight - popUp.Height - 30;
        }

        private void CloseForm()
        {
            this.Close();
            timerClose.Enabled = false;
        }

        #endregion

        #region Event handlers

        void timerClose_Elapsed(object sender, ElapsedEventArgs e)
        {
            Dispatcher.Invoke(closeForm);
        }

        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            timerClose.Enabled = true;
        }

        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            CloseForm();
        }

        #endregion


    }
}
